Library
library(tidyverse)
Load previous functions
# Function to set-up game
start_game <- function() {
# Create list of envelope stuffers
envelope_stuffers <- c("newsprint", "$1000")
for (i in 1:98) {
#print(i)
envelope_stuffers <- append(envelope_stuffers, "newsprint")
}
# Place them randomly in game envelopes
game_envelopes <- sample(envelope_stuffers, size=100, replace = F)
return(game_envelopes)
}
# Function for contestant's first choice
select_contestant_choice <- function() {
choice <- sample(1:100, size=1, replace = F)
return(choice)
}
# Function for host's choice
select_host_choice <- function(envelopes, contestant_choice) {
# Find indices of newsprint
newsprint_indices <- which(envelopes == "newsprint")
# Remove contestant choice index from options
host_envelopes <- newsprint_indices[!newsprint_indices %in% c(contestant_choice)]
host_choice <- sample(host_envelopes, size=98, replace = F)
host_choice <- c(host_choice)
return(host_choice)
}
# Contestant's final choice to keep original envelope or switch
final_contestant_choice <- function(envelopes, contestant_choice, host_choice, switch_choice) {
envelope_indices <- c(1:100)
final_envelope_index <- envelope_indices[!envelope_indices %in% c(contestant_choice, host_choice)]
final_envelope <- envelopes[final_envelope_index]
#print(switch_choice)
if (toupper(switch_choice) == "SWITCH") {
out <- final_envelope
} else {
out <- envelopes[contestant_choice]
}
return(out)
}
# Function to determine game outcome
game_outcome <- function(final_choice) {
if (final_choice == "$1000") {
return("WIN")
} else {
return("LOSE")
}
}
# Game play
monty_hall_100doors <- function(switch_choice) {
envelopes <- start_game()
contestant_choice <- select_contestant_choice()
host_choice <- sort(select_host_choice(envelopes, contestant_choice))
final_choice <- final_contestant_choice(envelopes, contestant_choice, host_choice, switch_choice)
outcome <- game_outcome(final_choice)
return(list(envelopes = envelopes,
contestant_choice = contestant_choice,
host_choice = host_choice,
switch_choice = switch_choice,
final_choice = final_choice,
outcome = outcome))
}
Review output
- Recall that we get the following output from our function:
monty_hall_100doors("KEEP")
## $envelopes
## [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [37] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [49] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [67] "newsprint" "newsprint" "newsprint" "newsprint" "$1000" "newsprint"
## [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [97] "newsprint" "newsprint" "newsprint" "newsprint"
##
## $contestant_choice
## [1] 49
##
## $host_choice
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
## [20] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
## [39] 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58
## [58] 59 60 61 62 63 64 65 66 67 68 69 70 72 73 74 75 76 77 78
## [77] 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
## [96] 98 99 100
##
## $switch_choice
## [1] "KEEP"
##
## $final_choice
## [1] "newsprint"
##
## $outcome
## [1] "LOSE"
monty_hall_100doors("SWITCH")
## $envelopes
## [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [37] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [49] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [73] "$1000" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
## [97] "newsprint" "newsprint" "newsprint" "newsprint"
##
## $contestant_choice
## [1] 74
##
## $host_choice
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
## [20] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
## [39] 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
## [58] 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 75 76 77 78
## [77] 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
## [96] 98 99 100
##
## $switch_choice
## [1] "SWITCH"
##
## $final_choice
## [1] "$1000"
##
## $outcome
## [1] "WIN"
Create play game function
With this new function, we want to play the entire game and save the outcome of keeping or switching.
play_game <- function( )
{
outcome.stay <- monty_hall_100doors("KEEP")$outcome
outcome.switch <- monty_hall_100doors("SWITCH")$outcome
# game.results <- bundle the results
# return( <<< game.results >>> )
strategy <- c("stay","switch")
outcome <- c(outcome.stay,outcome.switch)
game.results <- data.frame( strategy, outcome,
stringsAsFactors=F )
return( game.results )
}
play_game()
## strategy outcome
## 1 stay LOSE
## 2 switch WIN
Simulate results of game
results.df <- NULL # collector
for( i in 1:10000 ) # iterator
{
game.outcome <- play_game()
# binding step
results.df <- rbind( results.df, game.outcome )
}
table( results.df )
## outcome
## strategy LOSE WIN
## stay 9907 93
## switch 107 9893
( table( results.df ) / 10000 ) %>%
round( 2 )
## outcome
## strategy LOSE WIN
## stay 0.99 0.01
## switch 0.01 0.99
play_n_games <- function( n=100 )
{
library( dplyr )
results.list <- list() # collector
loop.count <- 1
for( i in 1:n ) # iterator
{
game.outcome <- play_game()
results.list[[ loop.count ]] <- game.outcome
loop.count <- loop.count + 1
}
results.df <- dplyr::bind_rows( results.list )
table( results.df ) %>%
prop.table( margin=1 ) %>% # row proportions
round( 2 ) %>%
print()
return( results.df )
}
play_n_games()
## outcome
## strategy LOSE WIN
## stay 0.97 0.03
## switch 0.01 0.99
## strategy outcome
## 1 stay LOSE
## 2 switch WIN
## 3 stay LOSE
## 4 switch WIN
## 5 stay LOSE
## 6 switch WIN
## 7 stay LOSE
## 8 switch WIN
## 9 stay LOSE
## 10 switch WIN
## 11 stay LOSE
## 12 switch WIN
## 13 stay LOSE
## 14 switch WIN
## 15 stay LOSE
## 16 switch WIN
## 17 stay LOSE
## 18 switch WIN
## 19 stay LOSE
## 20 switch WIN
## 21 stay LOSE
## 22 switch WIN
## 23 stay LOSE
## 24 switch WIN
## 25 stay LOSE
## 26 switch WIN
## 27 stay LOSE
## 28 switch WIN
## 29 stay LOSE
## 30 switch WIN
## 31 stay LOSE
## 32 switch WIN
## 33 stay LOSE
## 34 switch WIN
## 35 stay LOSE
## 36 switch WIN
## 37 stay LOSE
## 38 switch WIN
## 39 stay LOSE
## 40 switch WIN
## 41 stay LOSE
## 42 switch WIN
## 43 stay LOSE
## 44 switch WIN
## 45 stay LOSE
## 46 switch WIN
## 47 stay LOSE
## 48 switch WIN
## 49 stay LOSE
## 50 switch WIN
## 51 stay LOSE
## 52 switch WIN
## 53 stay LOSE
## 54 switch WIN
## 55 stay LOSE
## 56 switch WIN
## 57 stay WIN
## 58 switch WIN
## 59 stay LOSE
## 60 switch WIN
## 61 stay LOSE
## 62 switch WIN
## 63 stay LOSE
## 64 switch WIN
## 65 stay LOSE
## 66 switch WIN
## 67 stay LOSE
## 68 switch WIN
## 69 stay LOSE
## 70 switch WIN
## 71 stay WIN
## 72 switch WIN
## 73 stay WIN
## 74 switch WIN
## 75 stay LOSE
## 76 switch WIN
## 77 stay LOSE
## 78 switch WIN
## 79 stay LOSE
## 80 switch WIN
## 81 stay LOSE
## 82 switch WIN
## 83 stay LOSE
## 84 switch WIN
## 85 stay LOSE
## 86 switch WIN
## 87 stay LOSE
## 88 switch WIN
## 89 stay LOSE
## 90 switch WIN
## 91 stay LOSE
## 92 switch WIN
## 93 stay LOSE
## 94 switch WIN
## 95 stay LOSE
## 96 switch WIN
## 97 stay LOSE
## 98 switch WIN
## 99 stay LOSE
## 100 switch WIN
## 101 stay LOSE
## 102 switch WIN
## 103 stay LOSE
## 104 switch WIN
## 105 stay LOSE
## 106 switch WIN
## 107 stay LOSE
## 108 switch WIN
## 109 stay LOSE
## 110 switch WIN
## 111 stay LOSE
## 112 switch WIN
## 113 stay LOSE
## 114 switch WIN
## 115 stay LOSE
## 116 switch WIN
## 117 stay LOSE
## 118 switch WIN
## 119 stay LOSE
## 120 switch WIN
## 121 stay LOSE
## 122 switch WIN
## 123 stay LOSE
## 124 switch LOSE
## 125 stay LOSE
## 126 switch WIN
## 127 stay LOSE
## 128 switch WIN
## 129 stay LOSE
## 130 switch WIN
## 131 stay LOSE
## 132 switch WIN
## 133 stay LOSE
## 134 switch WIN
## 135 stay LOSE
## 136 switch WIN
## 137 stay LOSE
## 138 switch WIN
## 139 stay LOSE
## 140 switch WIN
## 141 stay LOSE
## 142 switch WIN
## 143 stay LOSE
## 144 switch WIN
## 145 stay LOSE
## 146 switch WIN
## 147 stay LOSE
## 148 switch WIN
## 149 stay LOSE
## 150 switch WIN
## 151 stay LOSE
## 152 switch WIN
## 153 stay LOSE
## 154 switch WIN
## 155 stay LOSE
## 156 switch WIN
## 157 stay LOSE
## 158 switch WIN
## 159 stay LOSE
## 160 switch WIN
## 161 stay LOSE
## 162 switch WIN
## 163 stay LOSE
## 164 switch WIN
## 165 stay LOSE
## 166 switch WIN
## 167 stay LOSE
## 168 switch WIN
## 169 stay LOSE
## 170 switch WIN
## 171 stay LOSE
## 172 switch WIN
## 173 stay LOSE
## 174 switch WIN
## 175 stay LOSE
## 176 switch WIN
## 177 stay LOSE
## 178 switch WIN
## 179 stay LOSE
## 180 switch WIN
## 181 stay LOSE
## 182 switch WIN
## 183 stay LOSE
## 184 switch WIN
## 185 stay LOSE
## 186 switch WIN
## 187 stay LOSE
## 188 switch WIN
## 189 stay LOSE
## 190 switch WIN
## 191 stay LOSE
## 192 switch WIN
## 193 stay LOSE
## 194 switch WIN
## 195 stay LOSE
## 196 switch WIN
## 197 stay LOSE
## 198 switch WIN
## 199 stay LOSE
## 200 switch WIN